package com.payment.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.ExcelUtil;
import com.payment.common.utils.StringUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.PlanItemNew;
import com.payment.system.domain.SysUser;
import com.payment.system.service.IPlanItemNewService;
import com.payment.system.service.ISysUserService;

/**
 * 新计划详情 信息操作处理
 * 
 * @author payment
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/system/planItemNew")
public class PlanItemNewController extends BaseController
{
    private String prefix = "system/planItemNew";

	@Autowired
	private ISysUserService userService;
	@Autowired
	private IPlanItemNewService planItemNewService;
	
	@RequiresPermissions("system:planItemNew:view")
	@GetMapping()
	public String planItemNew(ModelMap mmap)
	{
		Long userId= ShiroUtils.getUserId();
		String roleKey=userService.selectUserRoleKey(userId);
		mmap.put("role",roleKey);
	    return prefix + "/planItemNew";
	}
	
	/**
	 * 查询新计划详情列表
	 */
	@RequiresPermissions("system:planItemNew:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PlanItemNew planItemNew)
	{
		Long userId = ShiroUtils.getUserId();
		SysUser user = ShiroUtils.getSysUser();
		String roleKey = userService.selectUserRoleKey(userId);
		Map<String, Object> map = planItemNew.getParams();
		List<PlanItemNew> list = new ArrayList<>();
		if (StringUtils.equals(roleKey, "css")) {
			//客销部门登录
			map.put("css", "css");
			startPage();
			list = planItemNewService.selectPlanItemNewList1(planItemNew);
		} else if (StringUtils.equals(roleKey, "agent")) {
			//代理商部门登录
			map.put("agentId", user.getPhonenumber());
			startPage();
			list =planItemNewService.selectPlanItemNewList1(planItemNew);
		}else {
			startPage();
			list = planItemNewService.selectPlanItemNewList(planItemNew);
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出新计划详情列表
	 */
	@RequiresPermissions("system:planItemNew:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlanItemNew planItemNew)
    {
    	List<PlanItemNew> list = planItemNewService.selectPlanItemNewList(planItemNew);
        ExcelUtil<PlanItemNew> util = new ExcelUtil<PlanItemNew>(PlanItemNew.class);
        return util.exportExcel(list, "planItemNew");
    }
	
	/**
	 * 新增新计划详情
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存新计划详情
	 */
	@RequiresPermissions("system:planItemNew:add")
	@Log(title = "新计划详情", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PlanItemNew planItemNew)
	{		
		return toAjax(planItemNewService.insertPlanItemNew(planItemNew));
	}

	/**
	 * 修改新计划详情
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		PlanItemNew planItemNew = planItemNewService.selectPlanItemNewById(id);
		mmap.put("planItemNew", planItemNew);
		return "system/planNew/editPlanTime";
	}
	
	/**
	 * 修改保存新计划详情
	 */
	@RequiresPermissions("system:planItemNew:edit")
	@Log(title = "新计划详情", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PlanItemNew planItemNew)
	{
		planItemNew.setStatus(0);
		return toAjax(planItemNewService.updatePlanItemNew(planItemNew));
	}
	
	/**
	 * 删除新计划详情
	 */
	@RequiresPermissions("system:planItemNew:remove")
	@Log(title = "新计划详情", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(planItemNewService.deletePlanItemNewByIds(ids));
	}

}
